<?
class UserEditForm extends Form{
	var $user = array();
	function UserEditForm()
	{
		Form::Form('UserEditForm');
		$this->link_js('javascript/ajax.js');
		$this->link_js('javascript/jquery/packed/jquery.validate.js');	
		
		$id = Url::get('id');
		
		if($id){
			$this->user = DB::select("user","id=$id");
		}
		
		if(!$this->user)
		Url::redirect_current();
	}
	
	function on_submit()
	{
		$user_update 		= array();
		
		
		$full_name 			= Url::get('full_name');
		$user_name 			= Url::get('register_user_name');
		$mobile_phone 		= AZLib::trimSpace(Url::get('mobile_phone'));
		$home_phone 		= AZLib::trimSpace(Url::get('home_phone'));
		
		$gender 			= (int)Url::get('gender');
		$yahoo_id 			=  AZLib::trimSpace(Url::get('yahoo_id'));
		$skype_id 			=  AZLib::trimSpace(Url::get('skype_id'));
		$address 			=  AZLib::trimSpace(Url::get('address'));
		$website 			=  AZLib::trimSpace(Url::get('website'));
		
		$this->checkFormInput('Tên đầy đủ','full_name',$full_name,'str',false,'',0,50);	
		
		$this->checkFormInput('Điện thoại di động','mobile_phone',$mobile_phone,'str',false,'',0,50);	
		$this->checkFormInput('Điện thoại bàn','home_phone',$home_phone,'str',false,'',0,50);	
		
		$change_pass = 0;
		$change_mail = 0;
		
		if(User::is_root() && (User::id() ==1 || $this->user['id']!=1))
		{
			$email 				= Url::get('email');
			$password 			= AZLib::trimSpace(Url::get('register_password'));
			$confirm_password 	= AZLib::trimSpace(Url::get('confirm_password'));
			
			if($password && User::encode_password($password) != $this->user['password']){//Có nhập pass mới khác pass cũ
				$this->checkFormInput('Mật khẩu truy cập','register_password',$password,'str',true,'',6,50);
				
				$change_pass = 1;
				
				if($password != $confirm_password)
				{
					$this->setFormError('confirm_password',"Nhập lại Mật khẩu truy cập không khớp!");
				}	
			}
			
			if($email && $email != $this->user['email']){//Có nhập email mới khác email cũ
				$this->checkFormInput('Email','email',$email,'email',true,'',6,50);	
				
				$change_mail = 1;
			}
		}
		
		if($mobile_phone && !AZLib::is_mobile($mobile_phone)){
			$mobile_phone = "";
		}
		
		if(!$this->errNum)
		{
			if($change_mail && DB::exists('SELECT id FROM `user` WHERE `email`="'.$email.'" AND id!='.$this->user['id']))
			{
				$this->setFormError('email',"<b>Email</b> bạn chọn đã tồn tại, hãy chọn lại một <b>Email</b> khác!");
			}
			else{	
				$user_info = array(	
											'full_name'		=>$full_name,
											'mobile_phone'	=>$mobile_phone,
											'home_phone'	=>$home_phone,
											'gender'		=>($gender == 1?0:1),
											'yahoo_id'		=>$yahoo_id,
											'skype_id'		=>$skype_id,
											'address'		=>$address,
											'website'		=>$website,
											);

				if($change_pass)
				{
					$user_info['password'] = User::encode_password($password);
				}
				
				if($change_mail)
				{
					$user_info['email'] = $email;
				}
				
				DB::update('user', $user_info,"id=".$this->user['id']);
				
				User::getUser($this->user['id'],0,1);
				
				Url::redirect_current();
			}
		}
	}
	
	function draw(){
		global $display;	
		$this->beginForm();
		
		$display->add('msg_register',$this->showFormErrorMessages(1));			
				
		$display->add('error_full_name',	Url::get('full_name',$this->user['full_name']));
		$display->add('error_email',		Url::get('email',$this->user['email']));
		$display->add('user_name',			$this->user['user_name']);
		$display->add('error_mobile_phone',	Url::get('mobile_phone',$this->user['mobile_phone']));
		$display->add('error_home_phone',	Url::get('home_phone', $this->user['home_phone']));
		
		$display->add('register_password',	Url::get('register_password'));
		$display->add('confirm_password',	Url::get('confirm_password'));
		
		$display->add('error_gender',		(int)Url::get('gender',($this->user['gender']==0?1:2)));
		$display->add('error_yahoo_id',		Url::get('yahoo_id',$this->user['yahoo_id']));
		$display->add('error_skype_id',		Url::get('skype_id',$this->user['skype_id']));
		$display->add('error_address',		Url::get('address',$this->user['address']));
		$display->add('error_website',		Url::get('website',$this->user['website']));
		
		$display->output('Edit');	
		$this->endForm();		
	}
}
?>
